package Algorithm.string;

/**
 * @Desc:   最长回问子串
 * @author: cww
 * @DateTime: 2020-05-23 9:29
 */

public class MaxString {

    public static void main(String[] args) {

    }

    public static String maxString(String s){
        int len;
        if(s == null || (len = s.length()) <2) return s;
        boolean[][] dp = new boolean[len][len];
        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
        }
        int maxLen = 1,begin = 0;
        for (int j = 0; j < len; j++) {
            for (int i = 1; i < j; i++) {
                if (s.charAt(j) == s.charAt(i)){
                    if (j-i<3){
                        dp[j][i] = true;
                    } else {
                        dp[j][i] = dp[j+1][i-1];
                    }
                } else {
                    dp[j][i] = false;
                }
                if (dp[j][i]&&j-i+1>maxLen){
                    maxLen = j-i+1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, maxLen+begin);
    }
}
